package services

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"github.com/jinzhu/gorm"
	"go_motor_catalog/util"
	"strings"
)

const getSymcoCatalogUrl = "https://tw.sym-global.com/service"

const outSymcoBaseUrl = "https://tw.sym-global.com"

const getInSymcoCatalogUrl = "https://kymco.com.cn/index.php/index/Userinfo/qdjssclist"

const inSymcoBaseUrl = "http://www.xsmt.com/"

type SymcoOm struct {
}

type SymcoOmRes struct {
	ModelName       string
	ModelYear       string
	CatalogUrl      string
	CatalogShareUrl string
	Desc            string
}

func NewSymcoOm() *SymcoOm {
	return &SymcoOm{}
}

func (c *SymcoOm) getOutCatalog() []*SymcoOmRes {
	res := make([]*SymcoOmRes, 0)
	response := util.NewHttpUtil().GetRes(getSymcoCatalogUrl)
	dom, err := goquery.NewDocumentFromResponse(response)
	if err != nil {
		fmt.Println("获取kymco图纸数据失败->", err)
		return res
	}
	dom.Find(".tablepic").Each(func(i int, s *goquery.Selection) {
		if i == 1 {
			s.Find(".one").Each(func(j int, t *goquery.Selection) {
				val, exists := t.Find("a").Attr("href")
				if exists {
					res = append(res, &SymcoOmRes{
						ModelName:  strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(t.Find("a").Find(".textload").Text(), " ", ""), "使用說明書", ""), "使用", ""),
						ModelYear:  "0000",
						CatalogUrl: outSymcoBaseUrl + val,
					})
				}
			})
		}
	})
	return res
}
func (c *SymcoOm) getInCatalog() []*SymcoOmRes {
	res := make([]*SymcoOmRes, 0)
	urlSlice := []string{"http://www.xsmt.com/down-150-1.html", "http://www.xsmt.com/down-150-2.html", "http://www.xsmt.com/down-150-3.html"}
	for i := range urlSlice {
		url := urlSlice[i]
		response := util.NewHttpUtil().GetRes(url)
		dom, err := goquery.NewDocumentFromResponse(response)
		if err != nil {
			fmt.Println("获取kymco图纸数据失败->", err)
			continue
		}
		dom.Find(".aeltbc").Find("li").Each(func(i int, s *goquery.Selection) {
			val, exists := s.Find("a").Attr("href")
			if exists {
				res = append(res, &SymcoOmRes{
					ModelName:  strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s.Find(".d1").Text(), " ", ""), "使用说明书", ""), "说明书", ""), "說明書", ""),
					ModelYear:  strings.Split(s.Find(".d2").Text(), "-")[0],
					CatalogUrl: inSymcoBaseUrl + val,
				})
			}
		})
	}
	return res
}

func (c *SymcoOm) CrawData(db *gorm.DB) {
	catalogs := c.getOutCatalog()
	catalogs = append(catalogs, c.getInCatalog()...)
	for i := range catalogs {
		catalogInfo := catalogs[i]
		fmt.Printf("%s-%s-%s \n", catalogInfo.ModelName, catalogInfo.ModelYear, catalogInfo.CatalogUrl)
	}
	//modelOmDao := dao.NewManualModelOmDao(db)
	//catalogOmDao := dao.NewManualCatalogOmDao(db)
	//for i := range catalogs {
	//	param := catalogs[i]
	//	modelParam := &dao.ManualModelOm{
	//		ModelName: param.ModelName,
	//		ModelYear: param.ModelYear,
	//		BrandId:   9,
	//	}
	//	catalogParam := &dao.ManualCatalogOm{
	//		CatalogUrl:  param.CatalogUrl,
	//		CatalogDesc: "",
	//	}
	//	modelId := modelOmDao.CreateManualModelOm(modelParam)
	//	catalogParam.ModelId = modelId
	//	catalogOmDao.CreateManualCatalogOm(catalogParam)
	//	fmt.Println(param.ModelName, "-", param.ModelYear, "-successfull")
	//}
	fmt.Print("all successfull")
}
